package dao;

import java.util.ArrayList;
import java.util.Date;

import modelo.Servico;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class DaoServico {
	private SessionFactory fabrica;

	public DaoServico()throws Exception
	{	
	fabrica=new Configuration().addClass(modelo.Servico.class).buildSessionFactory();
	}
	public void cadastrar(Servico s)throws Exception
	{
		Session sessao=fabrica.openSession();
		Transaction transacao=sessao.beginTransaction();
		sessao.save(s);
		transacao.commit();
		sessao.flush();
		sessao.close();
	}
	public Servico consultar(Servico s)throws Exception
	{
		Session sessao=fabrica.openSession();
		Transaction transacao=sessao.beginTransaction();
		s=(Servico)sessao.get(modelo.Servico.class,s.getId_servico());
		transacao.commit();
		sessao.flush();
		sessao.close();
		return s;
	}

	public void excluir(Servico s)throws Exception
	{
		Session sessao=fabrica.openSession();
		Transaction transacao=sessao.beginTransaction();
		sessao.delete(s);
		transacao.commit();
		sessao.flush();
		sessao.close();
	}
	public void alterar(Servico s)throws Exception
	{
		Session sessao=fabrica.openSession();
		Transaction transacao=sessao.beginTransaction();
		sessao.update(s);
		transacao.commit();
		sessao.flush();
		sessao.close();
	}
	public ArrayList consultarTodos(){
		Session sessao=fabrica.openSession();
		Transaction transacao=sessao.beginTransaction();
		Query q=sessao.createQuery("from modelo.Servico");
		ArrayList servicos=(ArrayList)q.list();
		transacao.commit();
		sessao.flush();
		sessao.close();
		return servicos;
	}
	
	public ArrayList consultarRel(Date dataini, Date datafin){
		Session sessao=fabrica.openSession();
		Transaction transacao=sessao.beginTransaction();
		Query q=sessao.createQuery("from modelo.Servico where dt_servico >= " + dataini + " and dt_servico <= " + datafin);
		ArrayList servicos=(ArrayList)q.list();
		transacao.commit();
		sessao.flush();
		sessao.close();
		return servicos;
	}


	

}
